package com.tjvib.util;

import static com.tjvib.Constants.PRODUCT_RECHARGE1;
import static com.tjvib.Constants.PRODUCT_RECHARGE2;
import static com.tjvib.Constants.PRODUCT_RECHARGE3;
import static com.tjvib.Constants.PRODUCT_RECHARGE4;
import static com.tjvib.Constants.PRODUCT_RECHARGE5;
import static com.tjvib.Constants.SUCCESS;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import com.alipay.sdk.app.EnvUtils;
import com.alipay.sdk.app.PayTask;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tjvib.Constants;
import com.tjvib.R;
import com.tjvib.base.BaseActivity;
import com.tjvib.base.BaseFragment;
import com.tjvib.util.Manager.UserManager;
import com.tjvib.event.PayResultEvent;
import com.tjvib.pay.ali.AlipayResult;
import com.tjvib.util.retrofit.BaseResponse;
import com.tjvib.util.retrofit.RetrofitHelper;
import com.tjvib.view.dialog.LoadingDialog;
import com.tjvib.widget.AndroidScheduler;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class PayUtil {
    private static int selection = PRODUCT_RECHARGE1;
    private static final int SDK_PAY_FLAG_ZHIFUBAO = 220;


    public static void showProductDialog(BaseActivity activity) {
        activity.runOnUiThread(() -> {
            Dialog dialog = new Dialog(activity);
            View view = LayoutInflater.from(activity).inflate(R.layout.dialog_recharge_times, null);
            dialog.setContentView(view);
            dialog.setCanceledOnTouchOutside(false);
            dialog.show();

            RadioButton dialog_recharge_rb_p1 = view.findViewById(R.id.dialog_recharge_rb_p1);
            dialog_recharge_rb_p1.setOnCheckedChangeListener((compoundButton, b) -> {
                if(b) selection = PRODUCT_RECHARGE1;
            });
            RadioButton dialog_recharge_rb_p2 = view.findViewById(R.id.dialog_recharge_rb_p2);
            dialog_recharge_rb_p2.setOnCheckedChangeListener((compoundButton, b) -> {
                if(b) selection = PRODUCT_RECHARGE2;
            });
            RadioButton dialog_recharge_rb_p3 = view.findViewById(R.id.dialog_recharge_rb_p3);
            dialog_recharge_rb_p3.setOnCheckedChangeListener((compoundButton, b) -> {
                if(b) selection = PRODUCT_RECHARGE3;
            });
            RadioButton dialog_recharge_rb_p4 = view.findViewById(R.id.dialog_recharge_rb_p4);
            dialog_recharge_rb_p4.setOnCheckedChangeListener((compoundButton, b) -> {
                if(b) selection = PRODUCT_RECHARGE4;
            });
            RadioButton dialog_recharge_rb_p5 = view.findViewById(R.id.dialog_recharge_rb_p5);
            dialog_recharge_rb_p5.setOnCheckedChangeListener((compoundButton, b) -> {
                if(b) selection = PRODUCT_RECHARGE5;
            });

            RadioButton dialog_getvip_rb_ali_pay = view.findViewById(R.id.dialog_recharge_rb_ali_pay);
            RadioButton dialog_getvip_rb_wx_pay = view.findViewById(R.id.dialog_recharge_rb_wx_pay);

            Button dialog_rechange_btn_cancel = view.findViewById(R.id.dialog_rechange_btn_cancel);
            dialog_rechange_btn_cancel.setOnClickListener(v -> dialog.dismiss());

            Button dialog_rechange_btn_confirm = view.findViewById(R.id.dialog_rechange_btn_confirm);
            dialog_rechange_btn_confirm.setOnClickListener(v -> {
                LogUtil.e("selection="+selection);
                dialog.dismiss();
                if (dialog_getvip_rb_wx_pay.isChecked()){
                    switch (selection){
                        case PRODUCT_RECHARGE1:
                            genRechargeOrder(activity, selection, 1, 0, dialog);
                            break;
                        case PRODUCT_RECHARGE2:
                            genRechargeOrder(activity, selection, 9, 0, dialog);
                            break;
                        case PRODUCT_RECHARGE3:
                            genRechargeOrder(activity, selection, 40, 0, dialog);
                            break;
                        case PRODUCT_RECHARGE4:
                            genRechargeOrder(activity, selection, 70, 0, dialog);
                            break;
                        case PRODUCT_RECHARGE5:
                            genRechargeOrder(activity, selection, 100, 0, dialog);
                            break;
                    }
                }
                else if (dialog_getvip_rb_ali_pay.isChecked()){
                    switch (selection){
                        case PRODUCT_RECHARGE1:
                            genRechargeOrder(activity, selection, 1, 1, dialog);
                            break;
                        case PRODUCT_RECHARGE2:
                            genRechargeOrder(activity, selection, 9, 1, dialog);
                            break;
                        case PRODUCT_RECHARGE3:
                            genRechargeOrder(activity, selection, 40, 1, dialog);
                            break;
                        case PRODUCT_RECHARGE4:
                            genRechargeOrder(activity, selection, 70, 1, dialog);
                            break;
                        case PRODUCT_RECHARGE5:
                            genRechargeOrder(activity, selection, 100, 1, dialog);
                            break;
                    }
                }
            });
        });
    }

    public static void showProductDialog(BaseFragment fragment) {
        fragment.getActivity().runOnUiThread(() -> {
            Dialog dialog = new Dialog(fragment.getContext());
            View view = LayoutInflater.from(fragment.getContext()).inflate(R.layout.dialog_recharge_times, null);
            dialog.setContentView(view);
            dialog.setCanceledOnTouchOutside(false);
            dialog.show();

            RadioButton dialog_recharge_rb_p1 = view.findViewById(R.id.dialog_recharge_rb_p1);
            dialog_recharge_rb_p1.setOnCheckedChangeListener((compoundButton, b) -> {
                if(b) selection = PRODUCT_RECHARGE1;
            });
            RadioButton dialog_recharge_rb_p2 = view.findViewById(R.id.dialog_recharge_rb_p2);
            dialog_recharge_rb_p2.setOnCheckedChangeListener((compoundButton, b) -> {
                if(b) selection = PRODUCT_RECHARGE2;
            });
            RadioButton dialog_recharge_rb_p3 = view.findViewById(R.id.dialog_recharge_rb_p3);
            dialog_recharge_rb_p3.setOnCheckedChangeListener((compoundButton, b) -> {
                if(b) selection = PRODUCT_RECHARGE3;
            });
            RadioButton dialog_recharge_rb_p4 = view.findViewById(R.id.dialog_recharge_rb_p4);
            dialog_recharge_rb_p4.setOnCheckedChangeListener((compoundButton, b) -> {
                if(b) selection = PRODUCT_RECHARGE4;
            });
            RadioButton dialog_recharge_rb_p5 = view.findViewById(R.id.dialog_recharge_rb_p5);
            dialog_recharge_rb_p5.setOnCheckedChangeListener((compoundButton, b) -> {
                if(b) selection = PRODUCT_RECHARGE5;
            });

            RadioButton dialog_getvip_rb_ali_pay = view.findViewById(R.id.dialog_recharge_rb_ali_pay);
            RadioButton dialog_getvip_rb_wx_pay = view.findViewById(R.id.dialog_recharge_rb_wx_pay);

            Button dialog_rechange_btn_cancel = view.findViewById(R.id.dialog_rechange_btn_cancel);
            dialog_rechange_btn_cancel.setOnClickListener(v -> dialog.dismiss());

            Button dialog_rechange_btn_confirm = view.findViewById(R.id.dialog_rechange_btn_confirm);
            dialog_rechange_btn_confirm.setOnClickListener(v -> {
                LogUtil.e("selection="+selection);
                dialog.dismiss();
                if (dialog_getvip_rb_wx_pay.isChecked()){
                    switch (selection){
                        case PRODUCT_RECHARGE1:
                            genRechargeOrder(fragment, selection, 1, 0, dialog);
                            break;
                        case PRODUCT_RECHARGE2:
                            genRechargeOrder(fragment, selection, 9, 0, dialog);
                            break;
                        case PRODUCT_RECHARGE3:
                            genRechargeOrder(fragment, selection, 40, 0, dialog);
                            break;
                        case PRODUCT_RECHARGE4:
                            genRechargeOrder(fragment, selection, 70, 0, dialog);
                            break;
                        case PRODUCT_RECHARGE5:
                            genRechargeOrder(fragment, selection, 100, 0, dialog);
                            break;
                    }
                }
                else if (dialog_getvip_rb_ali_pay.isChecked()){
                    switch (selection){
                        case PRODUCT_RECHARGE1:
                            genRechargeOrder(fragment, selection, 1, 1, dialog);
                            break;
                        case PRODUCT_RECHARGE2:
                            genRechargeOrder(fragment, selection, 9, 1, dialog);
                            break;
                        case PRODUCT_RECHARGE3:
                            genRechargeOrder(fragment, selection, 40, 1, dialog);
                            break;
                        case PRODUCT_RECHARGE4:
                            genRechargeOrder(fragment, selection, 70, 1, dialog);
                            break;
                        case PRODUCT_RECHARGE5:
                            genRechargeOrder(fragment, selection, 100, 1, dialog);
                            break;
                    }
                }
            });
        });
    }

    private static void genRechargeOrder(BaseActivity activity, int product, double amount, int type, Dialog dialog) {
        LogUtil.e("pro="+product+ " amount="+amount);
        LoadingDialog loadingDialog = new LoadingDialog(activity);
        loadingDialog.showDialog(activity, "正在生成订单…");
        RetrofitHelper.getInstance().create()
                .gen("" + UserManager.getInstance().getUserId(), product, amount, type)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidScheduler.mainThread())
                .subscribe(new Observer<BaseResponse>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(BaseResponse value) {
                        loadingDialog.dismiss();
                        LogUtil.i("activity "+value.getCode()+" "+value.getMessage()+" "+value.getData());
                        if (value.getCode() == SUCCESS) {
                            if (type == 0)
                                createWechatOrder(activity, String.valueOf(value.getData()));
                            else if (type == 1)
                                createAlipayOrder(activity, String.valueOf(value.getData()), dialog);
                        } else
                            activity.showErrorDialog(value.getMessage());
                    }

                    @Override
                    public void onError(Throwable e) {
                        loadingDialog.dismiss();
                        LogUtil.i("activity "+e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }

    private static void genRechargeOrder(BaseFragment fragment, int product, double amount, int type, Dialog dialog) {
        LogUtil.e("pro="+product+ " amount="+amount);
        LoadingDialog loadingDialog = new LoadingDialog(fragment.getContext());
        loadingDialog.showDialog(fragment.getContext(), "正在生成订单…");
        RetrofitHelper.getInstance().create()
                .gen("" + UserManager.getInstance().getUserId(), product, amount, type)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidScheduler.mainThread())
                .subscribe(new Observer<BaseResponse>() {
                    @Override

                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(BaseResponse value) {
                        loadingDialog.dismiss();
                        LogUtil.i("fragment "+value.getCode()+" "+value.getMessage()+" "+value.getData());
                        if (value.getCode() == SUCCESS) {
                            if (type == 0)
                                createWechatOrder(fragment, String.valueOf(value.getData()));
                            else if (type == 1)
                                createAlipayOrder(fragment, String.valueOf(value.getData()), dialog);
                        } else
                            fragment.showErrorDialog(value.getMessage());
                    }

                    @Override
                    public void onError(Throwable e) {
                        loadingDialog.dismiss();
                        LogUtil.i("fragment "+e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }

    private static void createAlipayOrder(BaseActivity activity, String orderId, Dialog dialog) {
        LoadingDialog loadingDialog = new LoadingDialog(activity);
        loadingDialog.showDialog(activity, "正在请求支付宝…");
        RetrofitHelper.getInstance().create()
                .createAlipayOrder("" + UserManager.getInstance().getUserId(), orderId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidScheduler.mainThread())
                .subscribe(new Observer<BaseResponse>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseResponse value) {
                        loadingDialog.dismiss();
                        if (value.getCode() == SUCCESS) {
                            gotoAlipay(activity, String.valueOf(value.getData()), dialog);
                        } else {
                            activity.showErrorDialog(value.getMessage());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        loadingDialog.dismiss();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private static void createAlipayOrder(BaseFragment fragment, String orderId, Dialog dialog) {
        fragment.getActivity().runOnUiThread(() -> {
            LoadingDialog loadingDialog = new LoadingDialog(fragment.getContext());
            loadingDialog.showDialog(fragment.getContext(), "正在请求支付宝…");
            RetrofitHelper.getInstance().create()
                    .createAlipayOrder("" + UserManager.getInstance().getUserId(), orderId)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidScheduler.mainThread())
                    .subscribe(new Observer<BaseResponse>() {
                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onNext(BaseResponse value) {
                            loadingDialog.dismiss();
                            if (value.getCode() == SUCCESS) {
                                gotoAlipay(fragment, String.valueOf(value.getData()), dialog);
                            } else {
                                fragment.showErrorDialog(value.getMessage());
                            }
                        }

                        @Override
                        public void onError(Throwable e) {
                            loadingDialog.dismiss();
                            fragment.showErrorDialog(e.getMessage());
                        }

                        @Override
                        public void onComplete() {
                        }
                    });
        });
    }

    private static void createWechatOrder(BaseActivity activity, String orderId) {
        LoadingDialog loadingDialog = new LoadingDialog(activity);
        loadingDialog.showDialog(activity, "正在请求微信…");
        RetrofitHelper.getInstance().create()
                .createWechatOrder("" + UserManager.getInstance().getUserId(), orderId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidScheduler.mainThread())
                .subscribe(new Observer<BaseResponse>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseResponse value) {
                        loadingDialog.dismiss();
                        if (value.getCode() == SUCCESS) {
                            gotoWechat(activity, String.valueOf(value.getData()));
                        } else {
                            activity.showErrorDialog(value.getMessage());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        loadingDialog.dismiss();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private static void createWechatOrder(BaseFragment fragment, String orderId) {
        LogUtil.e("orderId=" + orderId);
        fragment.getActivity().runOnUiThread(() -> {
            LoadingDialog loadingDialog = new LoadingDialog(fragment.getContext());
            loadingDialog.showDialog(fragment.getContext(), "正在请求微信…");
            RetrofitHelper.getInstance().create()
                    .createWechatOrder("" + UserManager.getInstance().getUserId(), orderId)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidScheduler.mainThread())
                    .subscribe(new Observer<BaseResponse>() {
                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onNext(BaseResponse value) {
                            loadingDialog.dismiss();
                            if (value.getCode() == SUCCESS) {
                                gotoWechat(fragment, String.valueOf(value.getData()));
                            } else {
                                fragment.showErrorDialog(value.getMessage());
                            }
                        }

                        @Override
                        public void onError(Throwable e) {
                            loadingDialog.dismiss();
                            fragment.showErrorDialog(e.getMessage());
                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        });
    }

    private static void gotoAlipay(BaseActivity activity, String alipayOrderInfo, Dialog dialog) {
        new Thread(() -> {
            if (!isAlipayInstall(activity)) {
                ToastUtil.show("您还没有安装支付宝客户端");
                return;
            }
            PayTask alipay = new PayTask(activity);
            Map<String, String> result = alipay.payV2(alipayOrderInfo, true);
            LogUtil.e(result.toString());

            AlipayResult alipayResult = new AlipayResult(result);
            //String resultInfo = alipayResult.getResult();// 同步返回需要验证的信息
            String resultStatus = alipayResult.getResultStatus();
            String message;
            int status;

            if (TextUtils.equals(resultStatus, "9000")) {
                // 不做同步校验
                // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
                message = "订单支付成功";
                status = PayResultEvent.STATUS_SUCCESS;
                dialog.dismiss();
            } else if (TextUtils.equals(resultStatus, "8000") || TextUtils.equals(resultStatus, "6004")) {
                message = "正在处理中，支付结果未知，请稍后查询支付状态";
                status = PayResultEvent.STATUS_UNKNOWN;
            } else if (TextUtils.equals(resultStatus, "4000")) {
                message = "订单支付失败";
                status = PayResultEvent.STATUS_FAILURE;
            } else if (TextUtils.equals(resultStatus, "5000")) {
                message = "重复请求";
                status = PayResultEvent.STATUS_FAILURE;
            } else if (TextUtils.equals(resultStatus, "6001")) {
                message = "用户中途取消";
                status = PayResultEvent.STATUS_FAILURE;
            } else if (TextUtils.equals(resultStatus, "6002")) {
                message = "网络连接出错";
                status = PayResultEvent.STATUS_FAILURE;
            } else {
                message = "其他支付错误：" + alipayResult;
                status = PayResultEvent.STATUS_FAILURE;
            }

            Message msg = new Message();
            msg.what = SDK_PAY_FLAG_ZHIFUBAO;
            msg.obj = result;
            mHandler.sendMessage(msg);
            LogUtil.e("msg=" + message + "\nsta=" + status);

            EventBus.getDefault().post(new PayResultEvent(status, message));
        }).start();
    }

    private static void gotoAlipay(BaseFragment fragment, String alipayOrderInfo, Dialog dialog) {
        new Thread(() -> {
            if (!isAlipayInstall(fragment.getActivity())) {
                ToastUtil.show("您还没有安装支付宝客户端");
                return;
            }
            PayTask alipay = new PayTask(fragment.getActivity());
            Map<String, String> result = alipay.payV2(alipayOrderInfo, true);
            LogUtil.e(result.toString());

            AlipayResult alipayResult = new AlipayResult(result);
            //String resultInfo = alipayResult.getResult();// 同步返回需要验证的信息
            String resultStatus = alipayResult.getResultStatus();
            String message;
            int status;

            if (TextUtils.equals(resultStatus, "9000")) {
                // 不做同步校验
                // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
                message = "订单支付成功";
                ToastUtil.show("支付成功");
                status = PayResultEvent.STATUS_SUCCESS;
                dialog.dismiss();
            } else if (TextUtils.equals(resultStatus, "8000") || TextUtils.equals(resultStatus, "6004")) {
                message = "正在处理中，支付结果未知，请稍后查询支付状态";
                ToastUtil.show("正在处理中，请稍后");
                status = PayResultEvent.STATUS_UNKNOWN;
            } else if (TextUtils.equals(resultStatus, "4000")) {
                message = "订单支付失败";
                ToastUtil.show("支付失败");
                status = PayResultEvent.STATUS_FAILURE;
            } else if (TextUtils.equals(resultStatus, "5000")) {
                message = "重复请求";
                ToastUtil.show("重复请求");
                status = PayResultEvent.STATUS_FAILURE;
            } else if (TextUtils.equals(resultStatus, "6001")) {
                message = "用户中途取消";
                ToastUtil.show("取消支付");
                status = PayResultEvent.STATUS_FAILURE;
            } else if (TextUtils.equals(resultStatus, "6002")) {
                message = "网络连接出错";
                ToastUtil.show("网络连接出错");
                status = PayResultEvent.STATUS_FAILURE;
            } else {
                message = "其他支付错误：" + alipayResult;
                status = PayResultEvent.STATUS_FAILURE;
            }

            Message msg = new Message();
            msg.what = SDK_PAY_FLAG_ZHIFUBAO;
            msg.obj = result;
            mHandler.sendMessage(msg);
            LogUtil.e("msg=" + message + "\nsta=" + status);

            EventBus.getDefault().post(new PayResultEvent(status, message));
        }).start();
    }

    private static void gotoWechat(BaseActivity activity, String wechatOrderInfo) {
        new Thread(() -> {
            IWXAPI api = WXAPIFactory.createWXAPI(activity, Constants.APP_ID, true);
            api.registerApp(Constants.APP_ID);
            if (!isWechatInstall(activity)) {
                ToastUtil.show("您还没有安装微信客户端");
                return;
            }

            try {
                JSONObject jsonObject = new JSONObject(wechatOrderInfo);
                PayReq payReq = new PayReq();
                payReq.appId = Constants.APP_ID;
                payReq.partnerId = jsonObject.getString("partnerId");
                payReq.prepayId = jsonObject.getString("prepayId");
                payReq.packageValue = "Sign=WXPay";
                payReq.nonceStr = jsonObject.getString("nonceStr");
                payReq.timeStamp = jsonObject.getString("timestamp");
                payReq.sign = jsonObject.getString("sign");
                api.sendReq(payReq);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }).start();
    }

    private static void gotoWechat(BaseFragment fragment, String wechatOrderInfo) {
        LogUtil.e("wechatOrderInfo="+wechatOrderInfo);
        new Thread(() -> {
            IWXAPI api = WXAPIFactory.createWXAPI(fragment.getActivity(), Constants.APP_ID, true);
            api.registerApp(Constants.APP_ID);
            if (!isWechatInstall(fragment.getActivity())) {
                ToastUtil.show("您还没有安装微信客户端");
                return;
            }

            LogUtil.e("wechat go");
            try {
                LogUtil.e("wechat try");
                JSONObject jsonObject = new JSONObject(wechatOrderInfo);
                PayReq payReq = new PayReq();
                payReq.appId = Constants.APP_ID;
                payReq.partnerId = jsonObject.getString("partnerId");
                payReq.prepayId = jsonObject.getString("prepayId");
                payReq.packageValue = jsonObject.getString("packageVal");
                payReq.nonceStr = jsonObject.getString("nonceStr");
                payReq.timeStamp = jsonObject.getString("timestamp");
                payReq.sign = jsonObject.getString("sign");
                api.sendReq(payReq);
            } catch (JSONException e) {
                LogUtil.e("wechat exp" + e.getMessage());
                e.printStackTrace();
            }
        }).start();
    }


    /**
     * 是否安装支付宝
     */
    public static boolean isAlipayInstall(Context context) {
        Uri uri = Uri.parse("alipays://platformapi/startApp");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        ComponentName componentName = intent.resolveActivity(context.getPackageManager());
        return componentName != null;
    }


    /**
     * 是否安装微信
     */
    public static boolean isWechatInstall(Context context) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mm")) {
                    return true;
                }
            }
        }
        return false;
    }


    @SuppressLint("HandlerLeak")
    private static Handler mHandler = new Handler() {
        @SuppressWarnings("unused")
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SDK_PAY_FLAG_ZHIFUBAO: {
                    LogUtil.e("zfb SDK_PAY_FLAG_ZHIFUBAO");
                    break;
                }
                default:
                    break;
            }
        }
    };
}
